﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Data;

namespace DQYT.HTDZ.BusinessService
{
    public class ProcYC_File
    {

        public string m_TmpID;
        public string m_IdList;
        public FileUpload m_AccessoryUpload;
        public GridView m_Grid;
        public string m_Tab = "";


        /// <summary>
        /// 把文件临时保存到数据库中
        /// </summary>
        public void SaveTempFileToDB()
        {
            #region  代码实现
            string strMsg = "";
            //附件信息获取
            if (m_AccessoryUpload.PostedFile != null && m_AccessoryUpload.PostedFile.FileName != "")
            {
                string strFileFullName = m_AccessoryUpload.PostedFile.FileName;
                //获取纯文件名称，带扩展名
                string FileOrginName = CommFunc.GetOnlyFileName(strFileFullName);

                try
                {
                    OraDataService db = new OraDataService();
                    string strSql = "SELECT     * FROM        " + m_Tab + "   where 1=2";


                    OracleDataAdapter da = db.GetAdapter(strSql);
                    da.InsertCommand.Connection.Open();

                    DataSet dataSet = new DataSet();
                    da.Fill(dataSet, m_Tab);


                    DataTable dt = dataSet.Tables[0];
                    DataRow drow = null;

                    drow = dt.NewRow();
                    drow["ID"] = GetNewID();
                    drow["FILENAME"] = FileOrginName;
                    drow["TMPID"] = m_TmpID;

                    //获取文件流字节大小
                    long DOC_SIZE = this.m_AccessoryUpload.PostedFile.InputStream.Length;
                    //声明文件存取空间
                    byte[] BLOB_CONTENT = new byte[DOC_SIZE];
                    //读取文件流
                    m_AccessoryUpload.PostedFile.InputStream.Read(BLOB_CONTENT, 0, Convert.ToInt32(DOC_SIZE));
                    //获取文件体
                    drow["FILEDATA"] = BLOB_CONTENT;


                    dt.Rows.Add(drow);



                    da.Update(dataSet, m_Tab);
                    dataSet.AcceptChanges();
                    strMsg = drow["ID"].ToString();

                }
                catch (Exception Ex)
                {
                    strMsg = "ERROR:" + Ex.ToString();
                }
                DataTable dtFile = GetDataByIdListAndTempID(m_Tab, m_IdList, m_TmpID);
                m_Grid.DataSource = dtFile;
                m_Grid.DataBind();


            }

            #endregion  代码实现
        }




        /// <summary>
        /// 从序列发生器中获取一个主键值
        /// </summary>
        /// <returns></returns>
        public int GetNewID()
        {
            OraDataService ods = new OraDataService();
            string strSql = "select SEQ_YC_FILE.Nextval from dual";
            object o = ods.GetValue(strSql);
            return Convert.ToInt32(o);

        }


        /// <summary>
        /// 从指定的存储文件的表中按指定条件查询出数据
        /// </summary>
        /// <param name="p_Tab">存储文件的表，这个表格式固定</param>
        /// <param name="p_IdList">主键ID列表，形如：  1,3,7</param>
        /// <param name="p_TempID">临时ID</param>
        /// <returns>datatable</returns>
        public static DataTable GetDataByIdListAndTempID(string p_Tab, string p_IdList, string p_TempID)
        {
            #region 代码实现
            OraDataService ods = new OraDataService();
            string strSql = "select ID,FILENAME from " + p_Tab + " WHERE  ";// TMPID='' or ID IN (1,2)";
            if (string.IsNullOrEmpty(p_IdList))
            {
                if (string.IsNullOrEmpty(p_TempID))
                {
                    strSql += "1=2";
                }
                else
                {
                    strSql += "TMPID='" + p_TempID + "'";
                }
            }
            else
            {

                if (string.IsNullOrEmpty(p_TempID))
                {
                    strSql += "ID IN (" + p_IdList + ")";
                }
                else
                {
                    strSql += "ID IN (" + p_IdList + ")  or TMPID='" + p_TempID + "'";

                }
            }

            return ods.GetTable(strSql);
            #endregion 代码实现
        }

        /// <summary>
        /// 获得某个文件的数据
        /// </summary>
        /// <param name="p_Tab">指定表</param>
        /// <param name="p_ID">主键ID</param>
        /// <returns>文件的数据</returns>
        public static byte[] GetFileData(string p_Tab, string p_ID)
        {
            OraDataService ods = new OraDataService();
            string strSql = "select FILEDATA  from " + p_Tab + "  WHERE ID=" + p_ID;
            DataTable dt = ods.GetTable(strSql);
            if (dt.Rows.Count == 0)
                return null;
            else
            {
                byte[] a = (byte[])dt.Rows[0][0];
                return a;
            }
        }

        /// <summary>
        /// 获得某个文件的文件名
        /// </summary>
        /// <param name="p_Tab">指定表</param>
        /// <param name="p_ID">主键ID</param>
        /// <returns>文件名</returns>
        public static string GetFileName(string p_Tab, string p_ID)
        {
            OraDataService ods = new OraDataService();
            string strSql = "select FILENAME  from " + p_Tab + "  WHERE ID=" + p_ID;
            object o = ods.GetValue(strSql);
            if (o == null)
                return "";
            else
                return o.ToString();
        }

        /// <summary>
        /// 根据传入的文件主键列表查询出这些文件的基本信息(不包括数据体)
        /// </summary>
        /// <param name="p_Tab">指定表</param>
        /// <param name="p_IdList">主键列表,形如:1,4,6</param>
        /// <returns>基本信息表</returns>
        public static DataTable GetFilesByIdList(string p_Tab, string p_IdList)
        {
            if (string.IsNullOrEmpty(p_Tab) || string.IsNullOrEmpty(p_IdList))
                return null;

            OraDataService ods = new OraDataService();
            string strSql = "select ID,FILENAME,TMPID,BZ from " + p_Tab + "  WHERE ID IN (" + p_IdList + ")";
            DataTable dt = ods.GetTable(strSql);
            return dt;

        }

        /// <summary>
        /// 从上传的EXCEL文件中读取数据到DATATABLE 
        /// </summary>
        /// <returns>DataTable</returns>
        public DataTable GetDataTableFromExcelFile()
        {
            #region  代码实现

            //附件信息获取
            if (m_AccessoryUpload.PostedFile != null && m_AccessoryUpload.PostedFile.FileName != "")
            {
                return ProcExcel.GetExcelData(m_AccessoryUpload.PostedFile.InputStream);

            }
            else
                return null;

            #endregion  代码实现
        }

    }
}

